//
//  SISSGuaranteeCompanyTypeInViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/10/31.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSGuaranteeCompanyTypeInViewController.h"
#import "SISSGuaranteeCompanyShowViewController.h"
#import "SISSNetworkingManager+HouseProgress.h"

@interface SISSGuaranteeCompanyTypeInViewController ()

@property (weak, nonatomic) UILabel *companyNameTitleLabel;
@property (weak, nonatomic) UITextField *companyNameTextField;
@property (weak, nonatomic) UILabel *companyAddressTitleLabel;
@property (weak, nonatomic) UITextField *companyAddressTextField;
@property (weak, nonatomic) UIButton *confirmButton;

@end

@implementation SISSGuaranteeCompanyTypeInViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"担保公司";
    if (self.guaranteeCompanyModel) {
        self.companyNameTextField.text = self.guaranteeCompanyModel.organName;
        self.companyAddressTextField.text = self.guaranteeCompanyModel.organAddress;
    }
}

- (void)confirmButtonClickAction {
    
    if (!(self.companyNameTextField.text.length > 0)) {
        [SISSTools showSVWithTitle:@"请选择担保公司"];
        return;
    }
    if (!(self.companyAddressTextField.text.length > 0)) {
        [SISSTools showSVWithTitle:@"请输入担保公司地址"];
        return;
    }
    if (self.guaranteeCompanyModel) { // 修改
        NSDictionary *parameters = @{
                                     @"id": self.guaranteeCompanyModel.ID,
                                     @"spId": [SISSUserAccount shared].spId,
                                     @"soId": [SISSUserAccount shared].soId,
                                     @"ahbpId": self.ahbpId,
                                     @"organName": self.companyNameTextField.text,
                                     @"organAddress": self.companyAddressTextField.text
                                     };
        [kNetworkNoUTF8Manager updateGuaranteeOrganInfoWithParamters:parameters success:^(id responseObject) {
            if ([responseObject[@"code"] integerValue] == 200) {
                SISSGuaranteeCompanyShowViewController *vc = [[SISSGuaranteeCompanyShowViewController alloc] init];
                vc.ahbpId = self.ahbpId;
                [self.navigationController pushViewController:vc animated:YES];
            }
        } failed:^(NSError *error) {
            NSLog(@"%@",error);
        }];
    } else {
        NSDictionary *parameters = @{
                                     @"spId": [SISSUserAccount shared].spId,
                                     @"soId": [SISSUserAccount shared].soId,
                                     @"ahbpId": self.ahbpId,
                                     @"organName": self.companyNameTextField.text,
                                     @"organAddress": self.companyAddressTextField.text
                                     };
        [kNetworkNoUTF8Manager insertGuaranteeOrganInfoWithParamters:parameters success:^(id responseObject) {
            if ([responseObject[@"code"] integerValue] == 200) {
                SISSGuaranteeCompanyShowViewController *vc = [[SISSGuaranteeCompanyShowViewController alloc] init];
                vc.ahbpId = self.ahbpId;
                [self.navigationController pushViewController:vc animated:YES];
            }
        } failed:^(NSError *error) {
            NSLog(@"%@",error);
        }];
    }
    
}

- (void)setupSubviews {
    [super setupSubviews];
    
    UILabel *companyNameTitleLabel = [UILabel yyr_labelWithText:@"" fontSize:15 textColor:MAIN_TEXT_COLOR];
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:@"*担保公司名称："];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,1)];
    companyNameTitleLabel.attributedText = attributedStr;
    [self.view addSubview:companyNameTitleLabel];
    self.companyNameTitleLabel = companyNameTitleLabel;
    
    UITextField *companyNameTextField = [[UITextField alloc] init];;
    companyNameTextField.placeholder = @"请输入担保公司名称";
    companyNameTextField.layer.borderWidth = 1;
    companyNameTextField.layer.borderColor = UIColor.groupTableViewBackgroundColor.CGColor;
    [self.view addSubview:companyNameTextField];
    self.companyNameTextField = companyNameTextField;
    
    
    UILabel *companyAddressTitleLabel = [UILabel yyr_labelWithText:@"" fontSize:15 textColor:MAIN_TEXT_COLOR];
    NSMutableAttributedString *addressAttributedStr = [[NSMutableAttributedString alloc] initWithString:@"*担保公司地址："];
    [addressAttributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,1)];
    companyAddressTitleLabel.attributedText = addressAttributedStr;
    [self.view addSubview:companyAddressTitleLabel];
    self.companyAddressTitleLabel = companyAddressTitleLabel;
    
    UITextField *companyAddressTextField = [[UITextField alloc] init];
    companyAddressTextField.placeholder = @"请输入担保公司地址";
    companyAddressTextField.layer.borderWidth = 1;
    companyAddressTextField.layer.borderColor = UIColor.groupTableViewBackgroundColor.CGColor;
    [self.view addSubview:companyAddressTextField];
    self.companyAddressTextField = companyAddressTextField;
    
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    [confirmButton setBackgroundColor:MAIN_COLOR];
    [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmButton addTarget:self action:@selector(confirmButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    confirmButton.layer.cornerRadius = 4;
    confirmButton.layer.masksToBounds = YES;
    [self.view addSubview:confirmButton];
    self.confirmButton = confirmButton;
}

- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    [self.companyNameTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.mas_topLayoutGuideBottom).offset(globelMargin);
        make.left.equalTo(weakSelf.view).offset(2 * globelMargin);
    }];
    
    [self.companyNameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.companyNameTitleLabel);
        make.top.equalTo(weakSelf.companyNameTitleLabel.mas_bottom).offset(2 * globelMargin);
        make.right.equalTo(weakSelf.view).offset(-2 * globelMargin);
        make.height.mas_equalTo(5 * globelMargin);
    }];
    
    [self.companyAddressTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.companyNameTitleLabel);
        make.top.equalTo(weakSelf.companyNameTextField.mas_bottom).offset(4 * globelMargin);
    }];
    
    [self.companyAddressTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.companyNameTitleLabel);
        make.top.equalTo(weakSelf.companyAddressTitleLabel.mas_bottom).offset(2 * globelMargin);
        make.right.equalTo(weakSelf.view).offset(-2 * globelMargin);
        make.height.mas_equalTo(5 * globelMargin);
    }];
    
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.view).offset(2 * globelMargin);
        make.top.equalTo(weakSelf.companyAddressTextField.mas_bottom).offset(6 * globelMargin);
        make.right.equalTo(weakSelf.view).offset(-2 * globelMargin);
        make.height.mas_equalTo(5.5 * globelMargin);
    }];
}

@end
